﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.Common;
namespace SLLottery.Controls
{
    public partial class BetNum : UserControl
    {
        public BetNum()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(BetNum_Loaded);
        }
        public BetNum(int colNum, int rowNum)
            : this()
        {
            ColNum = colNum;
            RowNum = rowNum;
        }
        public int ColNum = 10;
        public int RowNum = 1;
        public string TitleText
        {
            set
            {
                this.Title.Text = value;
            }
            get
            {
                return this.Title.Text;
            }
        }
        public string GetChoosedNum()
        {
            string num = string.Empty;
            if (container.Children.Count > 1)
            {
                foreach (StackPanel sp in container.Children)
                {
                    foreach (NumButton nb in sp.Children)
                    {
                        num += nb.Tag == null ? "" : nb.Tag.ToString() + ",";
                    }
                }
                if (num.Length == 0)
                    return num;
                return num.Substring(0, num.Length - 1);
            }
            else
            {
                foreach (StackPanel sp in container.Children)
                {
                    foreach (NumButton nb in sp.Children)
                    {
                        num += nb.Tag == null ? "" : nb.Tag.ToString();
                    }
                }
                return num;
            }
        }
        void BetNum_Loaded(object sender, RoutedEventArgs e)
        {

        }
        public List<NumButton> GetNumButton()
        {
            List<NumButton> returnResult = new List<NumButton>();
            List<UIElement> list = container.Children.ToList();
            foreach (UIElement ui in list)
            {
                StackPanel sp = (StackPanel)ui;
                foreach (NumButton nb in sp.Children)
                {
                    returnResult.Add(nb);
                }
            }
            return returnResult;
        }
        public List<Button> GetCmdButton()
        {
            List<Button> returnResult = new List<Button>();
            foreach (Button btn in bt_Panel.Children)
            {
                returnResult.Add(btn);
            }
            return returnResult;
        }
        public void CreateElement()
        {
            this.Title.Width = 75;
            this.container.Width = 30 * ColNum;
            this.bt_Panel.Width = 150;
            int num = 0;
            if (ColNum != 10)
                num = 1;
            for (int i = 0; i < RowNum; i++)
            {
                StackPanel panel = new StackPanel();
                panel.Height = 30;
                panel.Orientation = Orientation.Horizontal;
                //panel.Margin = new Thickness(2);
                for (int j = 0; j < ColNum; j++)
                {
                    NumButton nb = new NumButton();
                    if (RowNum > 1 && num <= 9)
                    {
                        nb.Text = "0" + num.ToString();
                    }
                    else
                        nb.Text = num.ToString();
                    num++;
                    panel.Children.Add(nb);
                }
                this.container.Children.Add(panel);
            }

            this.bt_Panel.Children.Add(CreateButton("全"));
            this.bt_Panel.Children.Add(CreateButton("大"));
            this.bt_Panel.Children.Add(CreateButton("小"));
            this.bt_Panel.Children.Add(CreateButton("单"));
            this.bt_Panel.Children.Add(CreateButton("双"));
            this.bt_Panel.Children.Add(CreateButton("清"));
        }
        Button CreateButton(string text)
        {
            Button bt = new Button();
            bt.Content = text;
            bt.Margin = new Thickness(5, 0, 0, 0);
            bt.Height = 25;
            bt.Width = 20;
            bt.Style = App.Current.Resources["ButtonStyle1"] as Style;
            bt.Click += new RoutedEventHandler(bt_Click);
            return bt;
        }
        public void ClearNumStatus()
        {
            MainPage.dicWs.Remove(Title.Text);
            foreach (StackPanel sp in container.Children)
            {
                foreach (NumButton nb in sp.Children)
                {
                    nb.SetClicked(false);
                }
            }
        }
        private bool CanDoSelected()
        {
            bool flag = true;
            int zdws = 0;
            if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
            {
                if (MainPage.dicWs.Keys.Contains(Title.Text))
                {
                    MainPage.dicWs.Remove(Title.Text);
                    return true;
                }
                if (MainPage.playTypeRadio == 47)
                {
                    zdws = 3;
                }
                else
                {
                    zdws = 2;
                }
                if (MainPage.dicWs.Count >= zdws)
                {
                    flag = false;
                }
            }
            return flag;
        }
        void bt_Click(object sender, RoutedEventArgs e)
        {
            ClearNumStatus();
            Button bt = (Button)sender;
            switch (bt.Content.ToString())
            {
                case "全":
                    if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                    {
                        if (CanDoSelected())
                        {
                            MainPage.dicWs.Add(Title.Text, 10);
                        }
                        else
                            break;
                    }
                    foreach (StackPanel sp in container.Children)
                    {
                        foreach (NumButton nb in sp.Children)
                        {
                            nb.SetClicked(true);
                        }
                    }
                    
                    break;
                case "大":
                    if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                    {
                        if (CanDoSelected())
                        {
                            MainPage.dicWs.Add(Title.Text, 10);
                        }
                        else
                            break;
                    }
                    foreach (StackPanel sp in container.Children)
                    {
                        foreach (NumButton nb in sp.Children)
                        {
                            if (container.Children.Count > 1)//双色球
                            {
                                if (sp.Children.Count == 8)//篮球
                                {
                                    if (Convert.ToInt32(nb.Text) >= 9)
                                        nb.SetClicked(true);
                                }
                                else//红球
                                {
                                    if (Convert.ToInt32(nb.Text) >= 17)
                                        nb.SetClicked(true);
                                }
                            }
                            else
                            {
                                if (Convert.ToInt32(nb.Text) >= 5)
                                    nb.SetClicked(true);
                            }
                        }
                    }
                    
                    break;
                case "小":
                    if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                    {
                        if (CanDoSelected())
                        {
                            MainPage.dicWs.Add(Title.Text, 10);
                        }
                        else
                            break;
                    }
                    foreach (StackPanel sp in container.Children)
                    {
                        foreach (NumButton nb in sp.Children)
                        {
                            if (container.Children.Count > 1)//双色球
                            {
                                if (sp.Children.Count == 8)//篮球
                                {
                                    if (Convert.ToInt32(nb.Text) < 9)
                                        nb.SetClicked(true);
                                }
                                else//红球
                                {
                                    if (Convert.ToInt32(nb.Text) < 17)
                                        nb.SetClicked(true);
                                }
                            }
                            else
                            {
                                if (Convert.ToInt32(nb.Text) < 5)
                                    nb.SetClicked(true);
                            }
                        }
                    }
                    break;
                case "单":
                    if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                    {
                        if (CanDoSelected())
                        {
                            MainPage.dicWs.Add(Title.Text, 10);
                        }
                        else
                            break;
                    }
                    foreach (StackPanel sp in container.Children)
                    {
                        foreach (NumButton nb in sp.Children)
                        {
                            if (Convert.ToInt32(nb.Text) % 2 == 1)
                                nb.SetClicked(true);
                        }
                    }
                    break;
                case "双":
                    if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                    {
                        if (CanDoSelected())
                        {
                            MainPage.dicWs.Add(Title.Text, 10);
                        }
                        else
                            break;
                    }
                    foreach (StackPanel sp in container.Children)
                    {
                        foreach (NumButton nb in sp.Children)
                        {
                            if (Convert.ToInt32(nb.Text) % 2 == 0)
                                nb.SetClicked(true);
                        }
                    }
                    break;
                case "清":
                    if (MainPage.dicWs.Keys.Contains(Title.Text))
                    {
                        MainPage.dicWs.Remove(Title.Text);
                    }
                    foreach (StackPanel sp in container.Children)
                    {
                        foreach (NumButton nb in sp.Children)
                        {
                            nb.SetClicked(false);
                        }
                    }
                    break;
            }
        }
    }
}
